#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import sys
import time
import paramiko
import ConfigParser
import os
from retrying import retry

host = "fr.snowage.net"
user = "dataadmin"
password = "data123"
remote_path = "/home/dataadmin/missing_file"
rsync_path = "missing_file"
node = "7"


def getTime():
    return time.strftime('%Y%m%d %H:%M:%S', time.localtime())


def md5check(file):
    import commands

    cmd = "md5sum -c " + file + ".md5"
    result = commands.getoutput(cmd)
    if result.find("OK") != -1:
        print getTime() + " File " + file + " check OK."
        return True
    elif result.find("No such file or directory") != -1:
        print getTime() + " No such file " + file + ", retrying ... "
        return False
    else:
        print getTime() + " " + file + " is invalid, retrying ... "
        return False


def GetConfig(configfile):
    config = ConfigParser.ConfigParser()
    config.read(configfile)
    global host
    global user
    global password
    global remote_path
    global rsync_path
    global node
    host = config.get("node", "host")
    user = config.get("node", "user")
    password = config.get("node", "password")
    remote_path = config.get("node", "remote_path")
    rsync_path = config.get("node", "rsync_path")
    node = config.get("node", "node")


@retry
def downloadfile(file):
    if os.path.exists(file):
        os.remove(file)
    if os.path.exists(file + ".complete"):
        os.remove(file + ".complete")
    if os.path.exists(file + ".complete.node" + node):
        os.remove(file + ".complete.node" + node)
    if os.path.exists(file + ".md5"):
        os.remove(file + ".md5")
    os.system(
        "rsync -azcq --password-file=/etc/rsync.pass " + user + "@" + host + "::" + rsync_path + "/" + file + ".complete.node" + node + " ./")
    os.system(
        "rsync -azcq --password-file=/etc/rsync.pass " + user + "@" + host + "::" + rsync_path + "/" + file + ".md5 ./")
    os.rename(file + ".complete.node" + node, file)
    if not md5check(file):
        raise IOError("Invalid file.")


@retry
def deleteRemotefile(file):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host, port=22, username=user, password=password)
    ssh.exec_command("rm -f " + remote_path + "/" + file + ".complete.node" + node)
    ssh.exec_command("rm -f " + remote_path + "/" + file + ".md5")
    ssh.close()
    print getTime() + " " + file + " deleted."


if __name__ == "__main__":
    configfile = sys.argv[1]
    GetConfig(configfile)
    file = sys.argv[2]
    file = file[:-9]

    print getTime() + " Start downloading file " + file
    downloadfile(file)
    print getTime() + " " + file + " has been downloaded."
    deleteRemotefile(file)
    os.system("mv " + file + " ./files/")
    os.system("mv " + file + ".md5 ./files/")
